﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ELTE.TFEG.Hough.Controls
{
    /// <summary>
    /// Interaction logic for ImageThumbnailControl.xaml
    /// </summary>
    public partial class ImageThumbnailControl : UserControl
    {
        BitmapImage originalThumbImage;
        BitmapImage originalImage;
        FormatConvertedBitmap greyedImage;

        public bool Selected { get; set; }
        public BitmapImage Image { get { return originalImage; } }

        public ImageThumbnailControl(string Path = "")
        {
            InitializeComponent();
            if (!String.IsNullOrEmpty(Path))
            {
                this.Tag = Path;
                PopulateControl(path: Path);
            }
        }

        private void PopulateControl(string path)
        {
            try
            {
                originalThumbImage = new BitmapImage(new Uri(path.Replace(".jpg","_thumb.jpg")));
                originalImage = new BitmapImage(new Uri(path));
                greyedImage = new FormatConvertedBitmap(originalThumbImage, PixelFormats.Gray32Float, null, 0);
                Deselect();
            }
            catch (Exception ex)
            {
                originalThumbImage = new BitmapImage(new Uri("/Images/no-image.png", UriKind.Relative));
                imgThumb.Source = originalThumbImage;
                greyedImage = new FormatConvertedBitmap(originalThumbImage, PixelFormats.Gray32Float, null, 0);
            }
            
        }

        public void Deselect()
        {
            this.imgThumb.Source = greyedImage;
            Selected = false;
        }

        public void Select()
        {
            this.imgThumb.Source = originalThumbImage;
            Selected = true;
        }

        
    }
}
